package win.demonlegion.module;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 后台运营账号
 * Created by john on 2017/6/29.
 */
public class SysUserBean implements Serializable, UserDetails {
    private static final long serialVersionUID = -606333761495432691L;

    private String id; // 主键
    private String username; // 用户名
    private String password; // 密码
    private boolean status = true; // 账号状态
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    private Date modifyTime;
    private String role;
    private List<SysAuthorityBean> authorityList;
    private Collection<? extends GrantedAuthority> authorities;

    public SysUserBean() {}

    public SysUserBean(UserDetails userDetails) {
        this.username = userDetails.getUsername();
        this.password = userDetails.getPassword();
        this.authorities = userDetails.getAuthorities();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public List<String> getTokenAuthorities() {
        List<String> tokenAuthorities = new ArrayList<>();
        for(SysAuthorityBean sysAuthorityBean : authorityList) {
            tokenAuthorities.add(sysAuthorityBean.getAuthority());
        }
        return tokenAuthorities;
    }

    public void setAuthorityList(List<SysAuthorityBean> authorityList) {
        this.authorityList = authorityList;
        List<String> tokenAuthorities = new ArrayList<>();
        for(SysAuthorityBean sysAuthorityBean : authorityList) {
            tokenAuthorities.add(sysAuthorityBean.getAuthority());
        }
        setAuthorities(tokenAuthorities);
    }

    //返回分配给用户的角色列表
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(List<String> roleAuthorities) {
        authorities = roleAuthorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    // 账户是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    // 账户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    // 密码是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    // 账户是否激活
    @Override
    public boolean isEnabled() {
        return status;
    }
}
